package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChanneDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.enums.CustomException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional()
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {
    /**
     *频道列表
     */
    @Override
    public ResponseResult findListByQuery(ChanneDTO dto) {
        //1.校验
        dto.checkParam();
        //2.分页条件查询
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //2.1分页条件对象
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.<AdChannel>lambdaQuery();
        //2.2查询条件
        //判断name是否有值,有模糊查询
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
        //判断status是否有值 添加eq条件
        if(dto.getStatus()!=null){
            queryWrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        //添加排序条件
        queryWrapper.orderByAsc(AdChannel::getOrd);
        IPage<AdChannel> page = this.page(pageReq, queryWrapper);
        //3.封装返回结果
        return new PageResponseResult(dto.getPage(),dto.getSize(),page.getTotal(),page.getRecords());
    }

    /**
     *频道新增
     */
    @Override
    public ResponseResult insert(AdChannel channel) {
        //1.校验
        if (channel==null|| StringUtils.isBlank(channel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST);
        }
        //2.判断频道
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
        if (count>0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST,"该频道已存在");
        }
        //3.执行新增
        channel.setCreatedTime(new Date());
        save(channel);
        //4.返回结果
        return ResponseResult.okResult();
    }

    /*
     *修改频道及频道状态
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1 参数校验
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2 执行修改
        AdChannel channel = getById(adChannel.getId());
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
        }
        //3. 校验名称唯一性
        if(StringUtils.isNotBlank(adChannel.getName())
                &&
                !adChannel.getName().equals(channel.getName())){
            int count = this.count(Wrappers.<AdChannel>lambdaQuery()
                    .eq(AdChannel::getName, adChannel.getName()));
            if(count > 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
            }
        }
        updateById(adChannel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //1.校验参数
        if (id==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST);
        }
        AdChannel adChannel = getById(id);
        //2.判断频道是否存在和有效
        if (adChannel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //启动状态不能删除
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_STATUS_TRUE);
        }
        removeById(id);
//        if (id<100){ //如果id小于100 认为是业务错误
//           // throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST,"id不能小于100");
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"id不能小于100");
//        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}



